
var ajaxUrl = require('../../utils/url.js');
var app = getApp()
Page({
  data: {
    imgUrls: [
           
        ],
        page:0,
        categoryid:"",
        goodsList:[          
        ],
        response:"",
        loading:true,
        hidden:false
  },
  
  onLoad: function (options) {
    var that = this;
    //ajax请求数据
    that.setData({
      categoryid:options.categoryid
    });
    that.fetchImgListDate();
  },
  //跳转至详情页
  redictDetail:function(e){
    var goodid =  e.currentTarget.dataset.goodid;
    var link = "../goods/detail?goodid="+goodid;
    wx.navigateTo({
        url: link
      })
  },

  lower:function(e){
    var self = this;

    if (self.data.loading ){
      self.setData({
        page: self.data.page + 1
      });
    
      self.fetchImgListDate({page: self.data.page});
    }
  },
 
  fetchImgListDate:function(data){
    var self = this;
    self.setData({
      hidden: false
    });
   
      if (!data) data = {};
      if (!data.page) data.page = 0;
      if (data.page === 0) {
        self.setData({
          goodsList: []
        });
      }
      console.log("data page:"+data.page);
      console.log("page:"+self.data.page);
      wx.request({
          method:"GET",
          url:ajaxUrl.ajaxUrl()+"item",
          data: {
              "category":self.data.categoryid,
              "rows":20,
              "sortBy":'updatedAt',
              "sortOrder":1,
              "start":self.data.page *20
          },
          header: {
          'Authorization': ajaxUrl.getCiphertext()
          },
          success: function(res) {
            console.log(res.data)
             // var contentObj = [];
             console.log("res.data.code:"+res.data.code);
               if(res.data.code == 0){  
                  if(res.data.data.length == 0){
                     self.data.loading = false;
                     wx.showToast({
                        title: '已加载完全',
                        icon: 'success',
                        duration: 1500
                       })
                  }             
                  for(var i = 0;i < res.data.data.length;i++){
                    self.data.goodsList.push({
                        imgUrl: res.data.data[i].mainImage,
                        soldNum:res.data.data[i].soldNum,
                        price:res.data.data[i].inventories[0].adviceSalePrice,
                        name:res.data.data[i].name,
                        content:res.data.data[i].extra,
                        goodId:res.data.data[i]._id
                      });
                  }
               }
               self.setData({
                 goodsList:self.data.goodsList
               })
               setTimeout(function () {
                  self.setData({
                    hidden: true
                  });
              }, 300);
          }
      })
      
  },
  onShareAppMessage: function () {
    return {
      title: '恒泰云跨境购',
      desc: '商品列表',
      path: '/pages/goods/goods?categoryid='+this.data.categoryid
    };
  }


 
})
